package exp07gui.timer;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Date;

import javax.swing.JFrame;

public class Clock extends JFrame implements Runnable {
    /**
     *
     */
    private static final long serialVersionUID = 1L;
    final static int LEN_SECOND = 180;
    final static int LEN_MINUTE = 140;
    final static int LEN_HOUR = 100;
    double sAngle, mAngle, hAngle;

    @SuppressWarnings("deprecation")
    public Clock() {
        super("模拟时钟 ver 0.1 by ZHAO Jing");
        Date date = new Date();
        int h = date.getHours();
        int m = date.getMinutes();
        int s = date.getSeconds();
        hAngle = (h + m / 60. + s / 3600.) * 30 - 90;
        mAngle = (m + s / 60.) * 6 - 90;
        sAngle = s * 6 - 90;
        setSize(600, 600);
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    @Override
    public void paint(Graphics g) {
        g.setColor(Color.gray);
        g.fillRect(0, 0, getWidth(), getHeight());//填充背景

        Point center = new Point(getWidth() / 2, getHeight() / 2);
        Date date = new Date();
        int h = date.getHours();
        int m = date.getMinutes();
        int s = date.getSeconds();
        hAngle = (h + m / 60. + s / 3600.) * 30 - 90;
        mAngle = (m + s / 60.) * 6 - 90;
        sAngle = s * 6 - 90;
        g.setColor(Color.white);
        g.drawString(date.toLocaleString(), center.x - 50, 70);//绘画数字时钟
        int r = 200;
        g.setColor(Color.blue);
        g.drawArc(center.x - 5, center.y - 5, 10, 10, 0, 360);//绘画钟的外圆
        g.drawArc(center.x - r, center.y - r, 2 * r, 2 * r, 0, 360);//绘画圆心
        g.setColor(Color.black);
        for (int i = 0; i < 12; i++) {//绘画刻度
            int j = i + 10;
            g.drawString(
                    "" + (i + 1),
                    (int) (center.x + (r - 18)
                            * Math.cos((j * 30) / 180. * Math.PI)),
                    (int) (center.y + (r - 18)
                            * Math.sin((j * 30) / 180. * Math.PI)));
        }
        g.setColor(Color.red);//绘制时针、分针、秒针
        g.drawLine(
                center.x,
                center.y,
                (int) (center.x + (LEN_HOUR)
                        * Math.cos(hAngle / 180. * Math.PI)),
                (int) (center.y + (LEN_HOUR)
                        * Math.sin(hAngle / 180. * Math.PI)));
        g.setColor(Color.blue);
        g.drawLine(
                center.x,
                center.y,
                (int) (center.x + (LEN_MINUTE)
                        * Math.cos(mAngle / 180. * Math.PI)),
                (int) (center.y + (LEN_MINUTE)
                        * Math.sin(mAngle / 180. * Math.PI)));
        g.setColor(Color.yellow);
        g.drawLine(
                center.x,
                center.y,
                (int) (center.x + (LEN_SECOND)
                        * Math.cos(sAngle / 180.0 * Math.PI)),
                (int) (center.y + (LEN_SECOND)
                        * Math.sin(sAngle / 180.0 * Math.PI)));
    }

    public static void main(String[] args) {
        new Thread(new Clock()).start();//主程序（多线程）

    }

    @Override
    public void run() {//每隔一秒调用重绘方法(paint(Graphics))
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            repaint();

        }

    }

}
